package jp.ne.iforce.android.jsendtest;

import jp.ne.iforce.android.jsendtest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class EarthquakeActivity extends Activity  implements Runnable  {

	public String resStr;
	private Menu menu;

	private ProgressDialog progressDialog;
	private static final int MENU1 = 1;

	public C2dmSend c2dm = new C2dmSend();

	public Spinner lv_spinner;
	public Spinner sec_spinner;
	public Spinner tsunami_spinner;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.earthquake);

        //震度
        ArrayAdapter<?> lv_adapter = ArrayAdapter.createFromResource(this, R.array.Level_array, android.R.layout.simple_spinner_item);
        lv_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        lv_spinner = (Spinner) findViewById(R.id.Level_spin);
        lv_spinner.setAdapter(lv_adapter);

        //震度
        ArrayAdapter<?> sec_adapter = ArrayAdapter.createFromResource(this, R.array.Sec_array, android.R.layout.simple_spinner_item);
        sec_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sec_spinner = (Spinner) findViewById(R.id.Sec_spin);
        sec_spinner.setAdapter(sec_adapter);

        //震度
        ArrayAdapter<?> tsunami_adapter = ArrayAdapter.createFromResource(this, R.array.Tsunami_array, android.R.layout.simple_spinner_item);
        tsunami_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tsunami_spinner = (Spinner) findViewById(R.id.Tsunami_spin);
        tsunami_spinner.setAdapter(tsunami_adapter);

        // 送信ボタン
        ((Button) findViewById(R.id.Alert_button)).setOnClickListener(Alert_button);
    }

    public void startProgress(){
    	progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("送信中・・・");
		progressDialog.setIndeterminate(false);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.show();

    }
    public void run() {
    	c2dm.SendMessage("earthquake");
		handler.sendEmptyMessage(0);
	}


    private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
		}
	};

	private void sendMessage(){
		c2dm.Level = (String)lv_spinner.getSelectedItem();
		c2dm.Sec = (String)sec_spinner.getSelectedItem();
		c2dm.Tsunami = (String)tsunami_spinner.getSelectedItem();
		startProgress();
		Thread thread = new Thread(EarthquakeActivity.this);
		thread.start();
	}

    OnClickListener Alert_button = new View.OnClickListener() {
    	public void onClick(View v) {
    		sendMessage();
    	}
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu1) {
    	menu = menu1;
        // メニューアイテムを追加します
        menu.add(Menu.NONE, MENU1, Menu.NONE, "メッセージ");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean ret = true;
        switch (item.getItemId()) {
        default:
            ret = super.onOptionsItemSelected(item);
            break;
        case MENU1:
        	Intent i = new Intent(getApplicationContext(),JsendActivity.class);
        	startActivity(i);
        	finish();
        	break;
        }
        return ret;
    }

}